package com.mall.manager.babytop.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.WebUtils;

import com.mall.common.attributes.EnumType;
import com.mall.common.entity.Baby;
import com.mall.manager.babytop.service.BabyTopService;
import com.mall.untils.Page;

@SuppressWarnings({"unchecked","rawtypes"})
@RequestMapping("/manager")
@Controller
public class BabyTopController {

	@Resource
	BabyTopService babyTopService;
	
	/**
	 * 分页查询宝宝单题信息
	 * @param request
	 * @param page
	 * @return
	 */
	@RequestMapping("/pageBaby")
	public String findBabyTop(HttpServletRequest request,Page page,ModelMap modelMap){
		
		page = babyTopService.findBabyTop(page, request);
		JSONArray ar = JSONArray.fromObject(page.getResult());
		List<Baby> list = new ArrayList<Baby>();
		for(int i=0;i<ar.size();i++){
			Map<String,Object> map = JSONObject.fromObject(ar.get(i));
			Baby baby = new Baby();
			baby.setId(MapUtils.getInteger(map, "id"));
			baby.setBabyId(MapUtils.getInteger(map, "bady_id"));
			baby.setBabyName(MapUtils.getString(map, "baby_name")!=null?MapUtils.getString(map, "baby_name"):"无");
			baby.setTopicId(MapUtils.getInteger(map, "topic_id"));
			baby.setTopicName(MapUtils.getString(map, "topic_name")!=null?MapUtils.getString(map, "topic_name"):"无");
			baby.setGardenId(MapUtils.getInteger(map, "garden_id"));
			baby.setGardenName(MapUtils.getString(map, "garden_name")!=null?MapUtils.getString(map, "garden_name"):"无");
			baby.setTeacherId(MapUtils.getInteger(map, "teacher_id"));
			baby.setTeacherName(MapUtils.getString(map, "teacher_name")!=null?MapUtils.getString(map, "teacher_name"):"无");
			baby.setTopicTime(MapUtils.getInteger(map, "topic_time"));
			baby.setDataType(EnumType.DATA_TYPE_STATUS.getName(MapUtils.getInteger(map, "data_type")));
			list.add(baby);
		}
		Map paraMap = WebUtils.getParametersStartingWith(request, "");
		String dataType = MapUtils.getString(paraMap, "dataType");
		if(dataType!=null&&!dataType.equals("")){
			modelMap.addAttribute("dataType",dataType);
		}
		if(page!=null){
			int totalpagecount = page.getTotalPageCount();
	     	modelMap.addAttribute("totalpagecount", totalpagecount);
	     	int currentpageno = page.getCurrentPageNo();
	     	modelMap.addAttribute("currentpageno", currentpageno);
	     	int previouspage = page.getCurrentPageNo() <= 1 ? 1 : page
					.getCurrentPageNo() - 1;
	     	modelMap.addAttribute("previouspage", previouspage);
	     	int nextpage = page.getCurrentPageNo() >= page.getTotalPageCount() ? page
					.getTotalPageCount() : page.getCurrentPageNo() + 1;
			modelMap.addAttribute("nextpage", nextpage); 
			List<Integer> showNumList = page.getShowNumList();
			modelMap.addAttribute("pagenumlist", showNumList);
		}
		modelMap.addAttribute("data", list);
		modelMap.addAttribute("page", page);
		return "babytop/babytop_list";
	}
}
